package com.bobo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bobo.dao.GoodsRespository;
import com.bobo.domain.Brand;
import com.bobo.domain.Goods;
import com.bobo.domain.Product;
import com.bobo.domain.Trail;
import com.bobo.service.GoodsService;
import com.bobo.utils.HLUtils;
import com.github.pagehelper.PageInfo;

@Controller
public class GoodsController {
	
	
	@Autowired
	GoodsService goodsService;
	@Autowired
	RedisTemplate redisTemplate;
	@Autowired
	KafkaTemplate kafkaTemplate;
	
	@Autowired
	GoodsRespository goodsRespository;
	@Autowired
	ElasticsearchTemplate elasticsearchTemplate;
	//高亮搜索
	@RequestMapping("search")
	public String search(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "2") Integer pageSize, String value, Model model) {

		PageInfo info = HLUtils.search(elasticsearchTemplate, Goods.class, pageNum, pageSize,
				null, null, "goodsName",10,
				value, "goodsName", "pname","bname");

		model.addAttribute("info", info);
		model.addAttribute("value", value);

		return "search";

	}
	
	
	//删除
	@ResponseBody
	@PostMapping("deleteById")
	public  boolean deleteById(Integer id) {
		//获取执行修改的开始时间
				long t1 =System.currentTimeMillis();
				//1.更新msyql
				goodsService.deleteById(id);
				//2.删除es
				goodsRespository.deleteById(id);
				//获取修改执行完结束时间
				long t2 =System.currentTimeMillis();
				
				int t3=(int)(t2-t1);//操作耗时
				
				//获取自增id
				int tid=redisTemplate.opsForValue().increment("tid").intValue();
				Trail trail = new Trail(tid,"delete","/delete",JSON.toJSONString(id),t3,new Date());
				//发给kafka
				kafkaTemplate.sendDefault("log",JSON.toJSONString(trail));
				return true;
		
	}
	
	
	//获取所有分类
		@ResponseBody
		@GetMapping("getProducts")
		public List<Product> getProducts(){
			List<Product> list=null;
			if(redisTemplate.hasKey("list_product")) {//如果redis中有则取redis
			list =redisTemplate.opsForList().range("list_product", 0, -1);
			}else {
				 list = goodsService.ListProduct();//从数据库获取
				redisTemplate.opsForList().rightPushAll("list_product", list);//存入redis
			}
			return  list;
		}
	
	
	//获取所有品牌
	@ResponseBody
	@GetMapping("getBrands")
	public List<Brand> getBrands(){
		List<Brand> list=null;
		if(redisTemplate.hasKey("list_brand")) {//如果redis中有则取redis
		list =redisTemplate.opsForList().range("list_brand", 0, -1);
		}else {
			 list = goodsService.listBrand();//从数据库获取
			redisTemplate.opsForList().rightPushAll("list_brand", list);//存入redis
		}
		return  list;
	}
	
	
	//去增加
	@GetMapping("add")
	public String add() {
	return "add";
	}
	
	//执行增加
		@ResponseBody
		@PostMapping("add")
		public boolean add(Goods goods) {
			//获取执行的开始时间
			long t1 =System.currentTimeMillis();
			//1.更新msyql
			goodsService.add(goods);
			//2.更新es
			//es里面需要 保存 品牌名称和品类名称，所以需要单独查询一遍数据库
			Goods goods2 = goodsService.get(goods.getId());
			
			goodsRespository.save(goods2);
			//获取执行完结束时间
			long t2 =System.currentTimeMillis();
			
			int t3=(int)(t2-t1);//操作耗时
			
			//获取自增id
			int tid=redisTemplate.opsForValue().increment("tid").intValue();
			Trail trail = new Trail(tid,"add","/add",JSON.toJSONString(goods),t3,new Date());
			//发给kafka
			kafkaTemplate.sendDefault("log",JSON.toJSONString(trail));
			return true;
			
		}
	
	
	//去修改
	@GetMapping("update")
	public String update(Integer id,Model  model) {
	Goods goods=goodsService.get(id);
	model.addAttribute("goods", goods);
	return "update";
	}
	//执行修改
	@ResponseBody
	@PostMapping("update")
	public boolean update(Goods goods) {
		//获取执行修改的开始时间
		long t1 =System.currentTimeMillis();
		//1.更新msyql
		goodsService.update(goods);
		//2.更新es
		//es里面需要 保存 品牌名称和品类名称，所以需要单独查询一遍数据库
		Goods goods2 = goodsService.get(goods.getId());
		System.out.println(goods.getBname()+"===============");
		goodsRespository.save(goods2);
		//获取修改执行完结束时间
		long t2 =System.currentTimeMillis();
		
		int t3=(int)(t2-t1);//操作耗时
		
		//获取自增id
		int tid=redisTemplate.opsForValue().increment("tid").intValue();
		Trail trail = new Trail(tid,"update","/update",JSON.toJSONString(goods),t3,new Date());
		//发给kafka
		kafkaTemplate.sendDefault("log",JSON.toJSONString(trail));
		return true;
		
	}
	//1列表
	@RequestMapping("list")
	public String list(Goods goods,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam (defaultValue = "3") Integer pageSize,Model model) {
		//获取执行操作的开始时间
				long t1 =System.currentTimeMillis();
		
		PageInfo<Goods> info = goodsService.listGoods(goods, pageNum, pageSize);
		model.addAttribute("info", info);
		model.addAttribute("goods", goods);
		//获取操作执行完结束时间
		long t2 =System.currentTimeMillis();
		
		
		int t3=(int)(t2-t1);//操作耗时
		
		//获取自增id
		int tid=redisTemplate.opsForValue().increment("tid").intValue();
		
		Trail trail = new Trail(tid,"list","/list",JSON.toJSONString(goods),t3,new Date());
		//发给kafka
		kafkaTemplate.sendDefault("log",JSON.toJSONString(trail));
		return "list";
		
	}
	//获取操作统计信息
	@GetMapping("getTrailTotal")
	public String getTrailTotal(Model modal) {
		
		List<Trail> list = goodsService.getTrailTotal();
		modal.addAttribute("list", list);
		return "trailtotal";
	}
	//获取操作统计信息2
	@GetMapping("getTraiInfo")
	public String getTraiInfo(Model modal,Integer minute) {
		List<Trail> list = goodsService.getTraiInfo(minute);
		modal.addAttribute("list", list);
		return "trailinfo";
	}
}
